package com.ssyt.ec.sys.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ssyt.ec.common.AppServlet;
import com.ssyt.ec.common.util.DBUtil;
import com.ssyt.ec.common.util.FastjsonUtil;
import com.ssyt.ec.sys.entity.CourseEntity;

@WebServlet("/CourseServlet")
public class CourseServlet extends AppServlet {

	private static final long serialVersionUID = 1L;
	
	public CourseServlet() {
		super();
	}
	
	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String command = request.getParameter("command");
		if ("listMenu".equals(command)) {
			listMenu(request, response);
		} else if ("addCourse".equals(command)) {
			addCourse(request, response);
		} else if ("getCourse".equals(command)) {
			getCourse(request, response);
		} else if ("updateCourse".equals(command)) {
			updateCourse(request, response);
		} else if ("deleteCourse".equals(command)) {
			deleteCourse(request, response);
		} 
	}
	
	/**
	 * 主页查询所有课程
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void listMenu(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		int id = 0;
		String idStr = request.getParameter("id");
		
		if (idStr != null) {
			id = Integer.parseInt(idStr);
		}
		List<CourseEntity> courseList = DBUtil.executeQuery(CourseEntity.class,
				"SELECT co.*,us.name AS 'user__name' FROM sys_course co,sys_user us WHERE co.operate_user_id=us.id AND pid=?", id);

		print(response, FastjsonUtil.toJson(courseList));
	}
	
	/**
	 * 增加课程
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void addCourse(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		
        String name = request.getParameter("name");
        int operateUserId = Integer.parseInt(request.getParameter("operateUserId"));
        int state = Integer.parseInt(request.getParameter("state"));
        String memo = request.getParameter("memo");
        
        int row = DBUtil.executeUpdate("INSERT INTO sys_course VALUES(NULL, 1, 2, ?, ?, '20161102145210', ?, ?)", name, operateUserId, state, memo);
        if (row == 1) {
            print(response, 1);
        }
	}
	
	/**
	 * 获取课程信息
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	 private void getCourse(HttpServletRequest request,
			 HttpServletResponse response) throws IOException {
	        
		 	int id = Integer.parseInt(request.getParameter("id"));
		 	CourseEntity course = DBUtil.getUniqueResult(CourseEntity.class, "SELECT * FROM sys_course WHERE id=?", id);
	        if (course != null) {
	            print(response, FastjsonUtil.toJson(course));
	        }
	   }
	 
	/**
	 * 修改课程信息
	* @param request
	* @param response
	* @throws IOException
	*/
		private void updateCourse(HttpServletRequest request,
				HttpServletResponse response) throws IOException {
		      
			int id = Integer.parseInt(request.getParameter("id"));
		    String name = request.getParameter("name");
		    int pid = Integer.parseInt(request.getParameter("pid"));
		    int state = Integer.parseInt(request.getParameter("state"));
		      
		    int row = DBUtil.executeUpdate("UPDATE sys_course SET name=?,course_id=?,memo=? WHERE id=?", name, pid, state, id);

		      if (row == 1) {
		          print(response, 1);
		      }
		 }
		
	/**
	 * 删除课程
	 */
	private void deleteCourse(HttpServletRequest request,
				 HttpServletResponse response) throws IOException {
		        
			 String ids = request.getParameter("ids");
		     int row = DBUtil.executeUpdate("DELETE FROM sys_course WHERE id IN (" + ids + ")");

		      if (row >= 1) {
		          print(response, 1);
		   }
	 }
}
